package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.OrgClassTypeMapper;
import com.stylefeng.guns.common.persistence.model.OrgClassType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Arrays;
import java.util.List;

/**
 * 课程列表控制器
 *
 * @author fengshuonan
 * @Date 2018-08-22 09:49:20
 */
@Controller
@RequestMapping("/orgClassType")
public class OrgClassTypeController extends BaseController {

    private String PREFIX = "/system/orgClassType/";

    @Autowired
    OrgClassTypeMapper orgClassTypeMapper;
    /**
     * 跳转到课程列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "orgClassType.html";
    }

    /**
     * 跳转到添加课程列表
     */
    @RequestMapping("/orgClassType_add")
    public String orgClassTypeAdd() {
        return PREFIX + "orgClassType_add.html";
    }

    /**
     * 跳转到修改课程列表
     */
    @RequestMapping("/orgClassType_update/{orgClassTypeId}")
    public String orgClassTypeUpdate(@PathVariable Integer orgClassTypeId, Model model) {
        OrgClassType old=this.orgClassTypeMapper.selectById(orgClassTypeId);
        model.addAttribute("orgClassType",old);
        return PREFIX + "orgClassType_edit.html";
    }

    /**
     * 获取课程列表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<OrgClassType> orgClassTypes = orgClassTypeMapper.selectList(null);
        return orgClassTypes;
    }

    /**
     * 新增课程列表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(OrgClassType orgClassType) {
        orgClassTypeMapper.insert(orgClassType);
        return super.SUCCESS_TIP;
    }

    /**
     * 根据id获取课程名称
     */
    @RequestMapping(value = "/getClassName")
    @ResponseBody
    public Object getClassName(String ids) {
        if(null == ids) return null ;
        List<String> listIds = Arrays.asList(ids.split(","));
        String className = "";
        for (String id:listIds) {
            className = className + orgClassTypeMapper.selectById(id).getClassName() + ",";
        }
        if("" != className){
            className = className.substring(0,className.length()-1);
        }
        return className;
    }

    /**
     * 删除课程列表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除列表",key = "orgClassTypeId",dict = Dict.DeleteDict)
    public Object delete(@RequestParam Integer orgClassTypeId) {
        this.orgClassTypeMapper.deleteById(orgClassTypeId);
        return SUCCESS_TIP;
    }


    /**
     * 修改课程列表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改列表",key = "orgClassTypeId",dict = Dict.DictMap)
    public Object up(OrgClassType orgClassType) {
        Integer update = orgClassTypeMapper.updateById(orgClassType);
        return super.SUCCESS_TIP;
    }

    /**
     * 课程列表详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
